<?php

namespace App\Http\Controllers\Home\Orders;

use App\Http\Controllers\Home\Orders\Api\OrdersApi;
use App\Jobs\OrdersQueue;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Session;

class OrdersController extends Controller
{
	public $ordersApi;//OrdersApi 实例化生成的对象
	public function __construct()
	{
		$this->ordersApi = new OrdersApi();
	}

	public function confirmOrders(Request $request)
	{
		//调用订单处理API
		$res = $this->ordersApi->confirmOrders($request);
		if ( $res ) {
			return view('Home.Orders.orders')->with('data',$res);
		} else {
			return redirect()->route('login');
		}

    }

	public function pay(Request $request)
	{
		$payRes = $this->ordersApi->dealOrders($request);
		OrdersQueue::dispatch($this->ordersApi)->delay(Carbon::now()->addSeconds(10));
		if ( $payRes ) {
		    return view('Home.Orders.pay')->with('data',$payRes);
		} else {
			return view('Home.Orders.pay');
		}
    }

	public function dopay(Request $request,$oid)
	{
		$dopayRes = $this->ordersApi->dopay($oid);
		return view('Home.Orders.dopay');
    }

	public function dobuy(Request $request)
	{
		$gid = $request->gid;
		$pid = $request->pid;
		$num = $request->num;
		$res = $this->ordersApi->dobuy($gid, $pid, $num);
		if ( $res ) {
			return view('Home.Orders.orders')->with('data',$res);
		} else {
			return redirect()->route('login');
		}
    }


}
